import axios from "axios";
import router from "./router";
import ElementUI from "element-ui"

axios.defaults.baseURL = "http://localhost:8083"

const request = axios.create({
	timeout: 5000,
	headers: {
		'Content-Type': "application/json; charset=utf-8"
	}
})

request.interceptors.request.use(config => {
	config.headers['Authorization'] = localStorage.getItem("token")
	return config
})

request.interceptors.response.use(
	response => {

		console.log("response ->" + response)

		let res = response.data

		//踩坑记录，在这里的拦截器，由于后端接口无code返回，故不为200，走else，直接报错，导致Excel文件导出失败

		if(response.config.responseType == 'blob'){
			return response
		}
		if (res.code === 200) {
			ElementUI.Message.success(res.message)
			return response
		}
		// else {
		// 	ElementUI.Message.error(!res.message ? '系统异常 Ｏ(≧口≦)Ｏ' : res.message)
		// 	return Promise.reject(response.data.message)
		// }
		return response
	},
	// error => {
	//
	// 	console.log(error)
	//
	// 	if (error.response.data) {
	// 		error.massage = error.response.data.message
	// 	}
	//
	// 	if (error.response.status === 401) {
	// 		router.push("/login")
	// 	}
	//
	// 	ElementUI.Message.error(error.massage, {duration: 3000})
	// 	return Promise.reject(error)
	// }
)

export default request